package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

public class BaiduBbsAnalyse implements AnalyseNews {

	
	public boolean isDetailPage(String url) {
		String[] regex = {
				"http://tieba.baidu.com/p/[0-9]+.*",
				};
		return RegexUtils.matchAny(url, regex);
	}

	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		String commnetNum = null;
		String clickNum = null;
		String author="";
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select(".core_title_txt ").text();
		Element first = doc.select(".post-tail-wrap").first();
		if(first!=null){
			date = DateUtils.matchDate(first.text());
		}
		if(date==null){
			date = DateUtils.matchDate(doc.select("div.l_post_bright").attr("data-field"));
		}
		Elements select2 = doc.select("div.d_post_content");
		content = HtmlCleaner.getContentHtml(url, select2.first());
		Elements select = doc.select("div.d_author ul.p_author li.d_name a.p_author_name");
		if(select!=null && select.size()>0){
			author = select.get(0).text();
		}
			NewsMeta news = new NewsMeta();
			news.setAuthor(author);
			news.setUrl(url);
			news.setType(4);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(NumberUtils.parseInt(commnetNum));
			news.setClickNum(NumberUtils.parseInt(clickNum));
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setUpdateUrl(url);
			return news;
	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	public static void main(String[] args) {
		BaiduBbsAnalyse a = new BaiduBbsAnalyse();
		String url = "http://tieba.baidu.com/p/3863643606";
		url = UrlArgumentTop.FromatUrl(url);
		System.out.println(url);
		System.out.println(DoMainUtils.GetDomainName(url));
		if(a.isDetailPage(url)){
			UrlMeta meta = CrawlHTML.responseToURL(url);
			NewsMeta parserHtml = a.parserHtml(meta);
			System.out.println(parserHtml);
		}else{
			System.out.println("正则不匹配");
		}
	}

}
